var vm = new Vue({
    el: '#app',
    data:function(){
        return{
            pagename:"进度",
            common_fn:null,
            userinfo:null,
			searchType:true,
            loading:false,
			sf_loading:false,
            mainTable_maxHeight:0,
			search:{
                lotno:'',
                name:'',
                productno:'',
                creationdate:[]
			},
            data:[],
			dataClone:[],
			tempItem:null,
			sf_dialog:false,
            data_head:[
                {name:'批次号',property:'properties.lotno','filters':[]},
                {name:'产品名称',property:'properties.p_name','filters':[]},
                {name:'产品编号',property:'properties.productno','filters':[]},
                {name:'数量',property:'properties.lotnum','filters':[]},
                {name:'开始时间',property:'properties.creationdate','filters':[]},
                {name:'不良品数量',property:'properties.rejectquantity','filters':[]},
                {name:'总工时',property:'properties.totalhour','filters':[]},
                {name:'完成工时',property:'properties.realhour','filters':[]}
            ],
            sfData:[],
			sfData_head:[
				{name:'名称',property:'properties.name','filters':[]},
				{name:'实际数量',property:'properties.realquantity','filters':[]},
				{name:'订单数量',property:'properties.orderquantity','filters':[]},
				{name:'报工时间',property:'properties.reporttime','filters':[]},
				{name:'不良数量',property:'properties.rejectquantity','filters':[]},
				{name:'总工时',property:'properties.total_worktime','filters':[]},
				{name:'实际工时',property:'properties.real_worktime','filters':[]},
			],
            //分页-----------------
            // 总数量
            totalnum:0,
            // 总页数
            totalpage:0,
            // 每页数量
            everypagenum:20,
            // 当前页
            currentpage:1,
            //组件信息
            differentComponentDialog:null,
            differentComponentItem:null
        }
    },
    methods:{
        load_mesproductionschedule:function(){
            var self = this;
			var param = {
                sql:"select p.objid, p.lotno, p.sappo, p.name as p_name, p.productno, p.lotnum, p.creationdate, p.STARTDATE, ( select sum(a.ORDERQUANTITY*a.MANUFACTORYHOUR)  from( select sf.ORDERQUANTITY, (select ISNULL(PREPAREHOUR ,0)+ ISNULL(MANUFACTORYHOUR ,0) from qcp_process where objid = sf.PROCESS_OBJID) as MANUFACTORYHOUR from MES_SEMIFINISHED sf where sf.PRODUCT_OBJID = p.objid) a) as totalhour, (select sum(a.REALQUANTITY*a.MANUFACTORYHOUR) from( select ISNULL(sf.REALQUANTITY,0) as REALQUANTITY, (select ISNULL(PREPAREHOUR ,0)+ ISNULL(MANUFACTORYHOUR ,0) from qcp_process where objid = sf.PROCESS_OBJID) as MANUFACTORYHOUR from MES_SEMIFINISHED sf where sf.PRODUCT_OBJID = p.objid) a)  as realhour, (select sum(sf.REJECTQUANTITY) as REJECTQUANTITY from MES_SEMIFINISHED sf where sf.PRODUCT_OBJID = p.objid)  as REJECTQUANTITY,(select sum(sf.REALQUANTITY) as REALQUANTITY from MES_SEMIFINISHED sf where sf.PRODUCT_OBJID = p.objid) as realquantity ,(select sum(sf.ORDERQUANTITY) as ORDERQUANTITY from MES_SEMIFINISHED sf where sf.PRODUCT_OBJID = p.objid) as orderquantity  from MES_PRODUCTIONSCHEDULE p where p.STATE = 3",
                appName:"mes"
            }
			if(self.searchType){
				if(self.search.lotno != ''){
					param.sql+=" and p.lotno like '%"+ self.search.lotno +"%'";
                }
                if(self.search.name != ''){
					param.sql+=" and p.name like '%"+ self.search.name +"%'";
                }
                if(self.search.productno != ''){
					param.sql+=" and p.productno like '%"+ self.search.productno +"%'";
                }
                if(self.search.creationdate.length > 0){
					param.sql+=" and p.creationdate between '"+self.search.creationdate[0] +"' and '"+self.search.creationdate[1]+"'";
                }
			}
            self.loading = true;
            self.common_fn.sqlFn(param,function(response){
                if (response.resFlag == 0) {
                    self.data = [];
					self.dataClone = [];
                    for (var i = response.result.length-1; i>=0; i--) {
						var progress = Number(response.result[i].properties.realquantity) / Number(response.result[i].properties.orderquantity);
						isNaN(progress)==true?progress=0:1==1;
                        response.result[i].properties['progress'] = (progress * 100).toFixed(2);
						response.result[i].properties['progressColor'] = response.result[i].properties.progress==100?'success':'';
                    }
					self.totalnum = response.result.length;
					self.data = response.result;
                    self.dataClone = self.data.slice(0,self.everypagenum);
                    pushFilterData('data_head',self.data_head,self.dataClone);
                } else {
                    notify(response.errMsg,'error')
                }
                self.loading = false;
            })
        },
        load_process:function(row){
            var self = this;
            self.tempItem = row;
            var param = {
                sql:"select objid,name,OrderQuantity ,RealQuantity,(select Sum(ISNULL(RepairQuantity,0)) from MES_QACHECKITEM where PROCESS_OBJID = sf.PROCESS_OBJID and PRODUCT_OBJID = sf.PRODUCT_OBJID) as RejectQuantity,(select ISNULL(PREPAREHOUR ,0)+ ISNULL(MANUFACTORYHOUR ,0) from  qcp_process where objid = sf.process_objid) as ManufactoryHour,(select min(reporttime) from MES_REPORTSHEET where state=5 and SEMIFINISHED_OBJID = sf.objid) as reporttime from [MES_SEMIFINISHED] sf where sf.product_objid = "+self.tempItem.properties.objid,
                appName:"mes"
            }
            self.sf_loading = true;
			self.sf_dialog = true;
            self.common_fn.sqlFn(param,function(response){
                if (response.resFlag == 0) {
                    for (var i = response.result.length-1; i >=0; i--) {
                        if(Number(response.result[i].properties.orderquantity) == 0){ response.result.splice(i,1);continue }
						var progress = Number(response.result[i].properties.realquantity) / Number(response.result[i].properties.orderquantity);
						isNaN(progress)==true?progress=0:1==1;
                        response.result[i].properties['progress'] = (progress * 100).toFixed(2);
						response.result[i].properties['progressColor'] = response.result[i].properties.progress==100?'success':'';
						response.result[i].properties['total_worktime'] = response.result[i].properties.manufactoryhour*Number(self.tempItem.properties.lotnum).toFixed(2);
						response.result[i].properties['real_worktime'] = response.result[i].properties.manufactoryhour*Number(response.result[i].properties.realquantity).toFixed(2);
                        response.result[i].properties['url'] = "./progressDetail.html?productObjid="+self.tempItem.properties.objid+"&&semiFinishedObjid="+response.result[i].properties.objid;
                    }
                    self.sfData = response.result;
					pushFilterData('sfData_head',self.sfData_head,self.sfData);
                } else {
                    notify(response.errMsg,'error')
                }
                self.sf_loading = false;
            })
        },
		// 加载上一页或下一页
        handleCurrentChange:function(num){
            var self = this;
            self.currentpage = num;
			self.dataClone = self.data.slice((num-1)*self.everypagenum,num == self.totalpage?(self.totalnum):(self.everypagenum*num));
        },
		jump(row){
			window.location.href = row.properties.url;
		}
    },
    mounted:function(){
        var self = this;
        self.common_fn = new common_fn();
        self.common_fn.init(function(){
            self.common_fn.UserInfo(function(response){
                if(response.user == null){
                    window.location.href = '../login.html?redirect=./report/progresstable.html';
                }
                self.userinfo = response;
                self.common_fn.CheckFunction(self.userinfo,'mes','mes','Progress','','','progress',function(){})
                self.load_mesproductionschedule();
            })
        });
    }
})
window.onload =function(){
    if(vm.mainTable_maxHeight!=null ||vm.mainTable_maxHeight!=undefined){
        window.setTimeout(function(){
            vm.mainTable_maxHeight = window.screen.availHeight - document.body.clientHeight -70;
        },100)
    }
}
